<template>
  <div class="username-input-overlay" v-if="visible">
    <div class="username-input-dialog">
      <!-- 返回按钮 -->
      <div class="dialog-header">
        <div class="back-button" @click="handleCancel">
          <el-icon><ArrowLeft /></el-icon>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="dialog-body">
        <!-- 标签 -->
        <div class="input-label">我的名字</div>
        
        <!-- 用户名输入区域 -->
        <div class="username-input-wrapper">
          <input
            v-model="username"
            type="text"
            class="username-input"
            placeholder="请输入用户名"
            maxlength="20"
            @input="handleInput"
          />
          <div v-if="username" class="clear-button" @click="clearUsername">
            <el-icon><Close /></el-icon>
          </div>
        </div>
        <div class="input-divider"></div>
      </div>

      <!-- 确认按钮 -->
      <div class="dialog-footer">
        <button
          class="confirm-button"
          :class="{ 'disabled': !isValid }"
          :disabled="!isValid"
          @click="handleConfirm"
        >
          确定
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ArrowLeft, Close } from '@element-plus/icons-vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  initialUsername: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

const username = ref('')

// 验证用户名格式
const isValid = computed(() => {
  const name = username.value.trim()
  return name.length > 0 && name.length <= 20
})

// 监听visible变化，重置输入
watch(() => props.visible, (newVal) => {
  if (newVal) {
    username.value = props.initialUsername || ''
  }
})

const handleInput = (e) => {
  username.value = e.target.value
}

const clearUsername = () => {
  username.value = ''
}

const handleConfirm = () => {
  if (isValid.value) {
    emit('update:visible', false)
    emit('confirm', username.value.trim())
  }
}

const handleCancel = () => {
  emit('update:visible', false)
  emit('cancel')
}
</script>

<style lang="scss" scoped>
.username-input-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ffffff;
  z-index: 2000;
  
  .username-input-dialog {
    width: 100%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    
    .dialog-header {
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
      
      .back-button {
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        color: #000000;
        
        .el-icon {
          font-size: 20px;
        }
        
        &:active {
          opacity: 0.6;
        }
      }
    }
    
    .dialog-body {
      flex: 1;
      padding: 32px 20px 24px 20px;
      overflow-y: auto;
      
      .input-label {
        font-size: 14px;
        color: #909399;
        margin-bottom: 12px;
      }
      
      .username-input-wrapper {
        display: flex;
        align-items: center;
        position: relative;
        
        .username-input {
          flex: 1;
          border: none;
          background: transparent;
          font-size: 18px;
          color: #000000;
          outline: none;
          padding: 0;
          font-weight: 500;
          
          &::placeholder {
            color: #c0c4cc;
          }
        }
        
        .clear-button {
          width: 24px;
          height: 24px;
          border-radius: 50%;
          background: #c0c4cc;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          margin-left: 8px;
          flex-shrink: 0;
          
          .el-icon {
            font-size: 14px;
            color: #ffffff;
          }
          
          &:hover {
            background: #a8abb2;
          }
          
          &:active {
            background: #909399;
          }
        }
      }
      
      .input-divider {
        height: 1px;
        background: #f0f0f0;
        margin-top: 12px;
      }
    }
    
    .dialog-footer {
      padding: 16px 20px 24px 20px;
      border-top: 1px solid #f0f0f0;
      
      .confirm-button {
        width: 100%;
        height: 48px;
        border: none;
        border-radius: 8px;
        background: #ff6b9d;
        color: #ffffff;
        font-size: 16px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s;
        
        &:hover:not(.disabled) {
          background: #ff5a8a;
        }
        
        &:active:not(.disabled) {
          background: #ff4a7a;
        }
        
        &.disabled {
          background: #e4e7ed;
          color: #c0c4cc;
          cursor: not-allowed;
        }
      }
    }
  }
}
</style>

